home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / MacShell / DoCursor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-04  |  5.3 KB  |  182 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** Program:        MacShell
  5. ** File:        docursor.c
  6. ** Written by:  Eric Soldan
  7. **
  8. ** Copyright © 1990-1991 Apple Computer, Inc.
  9. ** All rights reserved.
  10. */
  11.  
  12.  
  13.  
  14. /*****************************************************************************/
  15.  
  16.  
  17.  
  18. #include "MacShell.h"            /* Get the MacShell includes/typedefs, etc.    */
  19. #include "MacShellCommon.h"        /* Get the stuff in common with rez.        */
  20. #include "MacShell.protos"        /* Get the prototypes for MacShell.            */
  21.  
  22. #ifndef __TEXTEDITCONTROL__
  23. #include "TextEditControl.h"
  24. #endif
  25.  
  26. #ifndef __TOOLUTILS__
  27. #include <ToolUtils.h>
  28. #endif
  29.  
  30. #ifdef THINK_C
  31. #include "Utilities.h"
  32. #else
  33. #ifndef __UTILITIES__
  34. #include <Utilities.h>
  35. #endif
  36. #endif
  37.  
  38.  
  39.  
  40. /*****************************************************************************/
  41.  
  42.  
  43.  
  44. RgnHandle    gCursorRgn;
  45.     /* The current cursor region.  The initial cursor region is an empty
  46.     ** region, which will cause WaitNextEvent to generate a mouse-moved
  47.     ** event, which will cause us to set the cursor for the first time.
  48.     */
  49.  
  50. Cursor    gCursor, *gCursorPtr;
  51.     /* The current cursor that applies to gCursorRgn.  These values
  52.     ** are here to shorten the re-processing time for determining the
  53.     ** correct cursor after an event.  This is specifically so that characters
  54.     ** can be typed into the TextEdit control faster.  If we spend a great
  55.     ** deal of time per-event recalculating the cursor region, text entry for
  56.     ** the TextEdit control slows down considerably.  If you want to override
  57.     ** the time savings because you are changing the cursor directly, either
  58.     ** set gCursorPtr to nil, or call DoSetCursor to set the cursor.
  59.     ** DoSetCursor simply sets gCursorPtr to nil, as well as setting
  60.     ** the cursor.
  61.     */
  62.  
  63.  
  64.  
  65. /*****************************************************************************/
  66. /*****************************************************************************/
  67.  
  68.  
  69.  
  70. /* Handle the cursor changes, based on if an AppleEvent is involved, or
  71. ** depending on the location of the cursor.  This also calculates the region
  72. ** where the current cursor resides (for WaitNextEvent).  If the mouse is
  73. ** ever outside of that region, an event would be generated, causing this
  74. ** function to be called, allowing us to change the region to the region
  75. ** the mouse is currently in.  The only other cursor management in this sample
  76. ** is for operations such as pulling down a menu.  Just prior to pulling down
  77. ** the menu, the arrow cursor is set.  This prevents any latencies in cursor
  78. ** update to cause a non-arrow cursor to be used in the menus.  This technique
  79. ** should be carried throughout the application.
  80. */
  81.  
  82. #pragma segment Main
  83. void    DoCursor(void)
  84. {
  85.     WindowPtr        window;
  86.     Point            mouseLoc;
  87. #if MACSHELL_VERSION
  88.     RgnHandle        rgn;
  89.     Rect            outBoxRct, teViewRct, contRct;
  90.     TEHandle        outBox, teHndl;
  91.     FileRecHndl        frHndl;
  92.     ControlHandle    viewCtl;
  93.     Cursor            **crsrHndl;
  94.     Point            contOrg;
  95. #endif
  96.  
  97.     mouseLoc = GetGlobalMouse();
  98.  
  99.     if ((!gInBackground) && (!IsDAWindow(window = FrontWindow()))) {
  100.  
  101.         if (IsAppWindow(window)) {
  102.  
  103.             if (gCursorPtr) {                            /* Do we already have a cursor... */
  104.                 if (PtInRgn(mouseLoc, gCursorRgn)) {    /* Are we still in the cursor area... */
  105.                     SetCursor(gCursorPtr);                /* Then set it to that. */
  106.                     return;
  107.                 }
  108.             }
  109.  
  110. #if MACSHELL_VERSION
  111.  
  112.             /* For the MacShell sample, we display an i-beam cursor when over the outbox. */
  113.  
  114.             frHndl    = (FileRecHndl)GetWRefCon(window);
  115.             viewCtl   = CTEViewFromTE(outBox = (*frHndl)->doc.outBox);
  116.             outBoxRct = (*viewCtl)->contrlRect;        /* Local coordinates of outbox. */
  117.             GetContentOrigin(window, &contOrg);        /* Scroll position of window. */
  118.             OffsetRect(&outBoxRct, -contOrg.h, -contOrg.v);
  119.                 /* By offseting the outbox rect by the amount scrolled, we now have
  120.                 ** the local position of the outbox within the potentially scrolled
  121.                 ** window content. */
  122.             GetContentRect(window, &contRct);
  123.                 /* This returns the content portion of the window in local coordinates,
  124.                 ** less document scrollbar area. */
  125.             SectRect(&outBoxRct, &contRct, &outBoxRct);
  126.                 /* Part of the outbox still visible after scrolling. */
  127.             LocalToGlobalRect(&outBoxRct);
  128.                 /* The outbox rect, in global coordinates. */
  129.  
  130.             if (CTETargetInfo(&teHndl, &teViewRct) == window) {
  131.                 /* If target TextEdit control belongs to this window... */
  132.  
  133.                 if ((teHndl == outBox) && (PtInRect(mouseLoc, &outBoxRct))) {
  134.                     /* If target TextEdit control is the outbox, and the cursor
  135.                     ** is over the visible part of the outbox... */
  136.  
  137.                     RectRgn(gCursorRgn, &outBoxRct);
  138.                         /* Set the cursor region to this rect. */
  139.  
  140.                     if (crsrHndl = GetCursor(ibeamCursor)) {
  141.                         gCursor = **crsrHndl;    /* Non-handle copy keeps it from moving. */
  142.                         SetCursor(gCursorPtr = &gCursor);
  143.                     }        /* Set the current cursor to the i-beam cursor resource. */
  144.                     return;
  145.                 }
  146.             }
  147.             SetRectRgn(gCursorRgn, kExtremeNeg, kExtremeNeg, kExtremePos, kExtremePos);
  148.             rgn = NewRgn();
  149.             RectRgn(rgn, &outBoxRct);
  150.             DiffRgn(gCursorRgn, rgn, gCursorRgn);
  151.             DisposeRgn(rgn);
  152.  
  153. #endif
  154.  
  155.         }
  156.  
  157.         DoSetCursor(&qd.arrow);
  158.     }
  159.  
  160.     else {
  161.         SetRectRgn(gCursorRgn, kExtremeNeg, kExtremeNeg, kExtremePos, kExtremePos);
  162.         gCursorPtr = nil;
  163.     }
  164. }
  165.  
  166.  
  167.  
  168. /*****************************************************************************/
  169.  
  170.  
  171.  
  172. #pragma segment Main
  173. void    DoSetCursor(Cursor *cursor)
  174. {
  175.     if (cursor) SetCursor(cursor);
  176.     gCursorPtr = nil;
  177.     if (!cursor) DoCursor();
  178. }
  179.  
  180.  
  181.  
  182.